function sleep_and_log(duration) {
  var interval = 5;
  while (duration > interval) {
    sleep(interval * 1000);
    console.log("sleep_and_log: interval ", interval, "s");
    duration = duration - interval;
  }
  if (duration > 0) {
    sleep(duration * 1000);
    console.log("sleep_and_log: duration ", duration, "s");
  }
}
function click_text(content) {
  if (exists_text(content)) {
    var uiText = text(content).findOne();
    var bounds = uiText.bounds();
    click(bounds.centerX(), bounds.centerY());
    return uiText;
  } else {
    return false;
  }
}
function click_text_starts_with(content) {
  if (exists_text_starts_with(content)) {
    var uiText = textStartsWith(content).findOne();
    var bounds = uiText.bounds();
    click(bounds.centerX(), bounds.centerY());
    return uiText;
  } else {
    return false;
  }
}
function exists_text(content) {
  if (text(content).exists()) {
    return text(content).findOne();
  } else {
    return false;
  }
}
function exists_text_starts_with(content) {
  if (textStartsWith(content).exists()) {
    return textStartsWith(content).findOne();
  } else {
    return false;
  }
}
function get_center_point(x1, y1, x2, y2) {
  return {
    x: x1 + (x2 - x1) / 2,
    y: y1 + (y2 - y1) / 2,
  };
}
function click_point(x1, y1, x2, y2) {
  var point = get_center_point(x1, y1, x2, y2);
  return click(point.x, point.y);
}
function click_bounds(bounds) {
  click(bounds.centerX(), bounds.centerY());
}
function click_ui(ui_item) {
  while (true) {
    if (ui_item && ui_item.clickable()) {
      ui_item.click();
      return true;
    } else if (ui_item) {
      ui_item = ui_item.parent();
    } else {
      return false;
    }
  }
}
function swipe_up(duration, distance) {
  while (duration > 3) {
    swipe(
      device.width / 2,
      device.height / 2,
      device.width / 2,
      device.height / 2 - distance,
      500 + random(0, 10)
    );
    var interval = 1 + random(0, 2);
    sleep(interval * 1000);
    duration = duration - interval;
  }
  if (duration > 0) {
    sleep(duration * 1000);
  }
  utils.toast("swipe_up end");
}
function utils_toast(content) {
  console.log(content);
  toast(content);
  sleep(6 * 1000);
}
function utils_sleep(duration) {
  var duration = duration / 1000;
  while (duration > 4) {
    sleep(4 * 1000);
    duration = duration - 4;
  }
  if (duration > 0) {
    sleep(duration * 1000);
  }
}
function get_gold(content) {
  var reg_exp = /(\d+)/g;
  var gold_match = content.match(reg_exp);
  var gold = parseInt(gold_match ? gold_match[0] : 0);
  return gold;
}
function get_size_point(points) {
  var x1 = points[0];
  var y1 = points[1];
  var x2 = points[2];
  var y2 = points[3];
  var rate_x = device.width / 720;
  var rate_y = device.height / 1650;
  return [
    parseInt(x1 * rate_x),
    parseInt(y1 * rate_y),
    parseInt(x2 * rate_x),
    parseInt(y2 * rate_y),
  ];
}
function click_size_x_y(x, y) {
  var rate_x = device.width / 720;
  var rate_y = device.height / 1650;
  click(parseInt(x * rate_x), parseInt(y * rate_y));
}

var utils = {
  sleep_and_log,
  click_text,
  click_text_starts_with,
  exists_text,
  exists_text_starts_with,
  get_center_point,
  click_point,
  click_bounds,
  click_ui,
  click_size_x_y,
  swipe_up,
  toast: utils_toast,
  sleep: utils_sleep,
  get_gold,
  get_size_point,
};

var point_map = {
  首页视频: [147, 1534, 278, 1618],
  首页百度: [16, 1534, 147, 1618],
  首页任务: [278, 1534, 441, 1618],
  进入任务之后的关闭: [582, 518, 606, 542],
  开宝箱之后的关闭: [550, 288, 606, 344],
  开福袋之后的关闭: [632, 344, 686, 400],
  首页福利: [440, 1502, 574, 1618],
};

var MINI_GOLD = 15;
var treasure = {
  go_page_from_home() {
    if (
      !!utils.exists_text("百度") &&
      !!utils.exists_text("听书") &&
      !!utils.exists_text("福利")
    ) {
      console.log("treasure:go_page_from_home");
      utils.click_point.apply(
        utils,
        utils.get_size_point(point_map["首页福利"])
      );
      sleep(2 * 1000);
      this.start();
    } else {
      console.log("treasure:go_page_from_home: NOT FOUND");
    }
  },
  start() {
    var ui_text =
      utils.exists_text_starts_with("再看一个领取") ||
      utils.exists_text_starts_with("观看视频再得") ||
      utils.exists_text_starts_with("看广告再");
    if (ui_text) {
      console.log(
        "treasure start: 找到【再看一个领取】或【观看视频再得】或【看广告再】"
      );
      var gold = utils.get_gold(ui_text.text());
      if (gold > MINI_GOLD) {
        if (utils.click_text_starts_with("再看一个领取")) {
        }
        if (utils.click_text_starts_with("观看视频再得")) {
        }
        if (utils.click_text_starts_with("看广告再")) {
        }
        sleep(2 * 1000);
        this.execute();
        return;
      } else {
        utils.click_point.apply(
          utils,
          utils.get_size_point(point_map["进入任务之后的关闭"])
        );
        sleep(2 * 1000);
      }
    }
    if (utils.click_text("立即收下")) {
      console.log("treasure start: 找到【立即收下】");
      sleep(2 * 1000);
    }
    var ui_text = utils.click_text("开宝箱得金币");
    if (ui_text) {
      console.log("treasure start: 找到【开宝箱得金币】");
      sleep(2 * 1000);
      this.init();
    } else {
      console.log("treasure start: 没找到【开宝箱得金币】");
    }
  },
  init() {
    var ui_text =
      utils.exists_text_starts_with("再看一个领取") ||
      utils.exists_text_starts_with("观看视频再得") ||
      utils.exists_text_starts_with("看广告再");
    if (ui_text) {
      console.log(
        "treasure init: 找到【再看一个领取】或【观看视频再得】或【看广告再】"
      );
      var gold = utils.get_gold(ui_text.text());
      console.log("treasure init: " + gold + "金币");
      if (gold > MINI_GOLD) {
        if (utils.click_text_starts_with("再看一个领取")) {
        }
        if (utils.click_text_starts_with("观看视频再得")) {
        }
        if (utils.click_text_starts_with("看广告再")) {
        }
        sleep(2 * 1000);
        this.execute();
      } else {
        utils.click_point.apply(
          utils,
          utils.get_size_point(point_map["开宝箱之后的关闭"])
        );
        sleep(2 * 1000);
        this.next();
      }
      return;
    }
    console.log(
      "treasure init: 没找到【再看一个领取】或【观看视频再得】或【看广告再】"
    );
    this.next();
  },
  execute() {
    sleep((30 + random(5, 7)) * 1000);
    console.log("treasure execute: end");
    if (utils.exists_text("查看详情") && !utils.exists_text("立即下载")) {
      if (
        utils.exists_text("查看详情").clickable() &&
        utils.click_text("查看详情")
      ) {
        console.log("treasure execute: 找到【查看详情】");
        utils.swipe_up(5 + random(1, 3), 100);
        back();
        sleep(2 * 1000);
        back();
      } else {
        console.log("treasure execute: 没找到【查看详情】");
        back();
      }
    } else {
      console.log("treasure execute: 没找到【查看详情】");
      back();
    }
    this.after();
  },
  after() {
    sleep(2 * 1000);
    var ui_text_1 = utils.exists_text_starts_with("再看一个领取");
    if (ui_text_1) {
      console.log("treasure after: 找到【再看一个领取】");
      var gold = utils.get_gold(ui_text_1.text());
      console.log("treasure after: " + gold + "金币");
      if (gold > MINI_GOLD) {
        utils.click_text_starts_with("再看一个领取");
        sleep(2 * 1000);
        this.execute();
      } else {
        if (utils.click_text("残忍离开")) {
          console.log("treasure after: 找到【残忍离开】");
          sleep(2 * 1000);
        } else {
          console.log("treasure after: 没找到【残忍离开】");
        }
        this.next();
      }
    }
    var ui_text_2 = utils.exists_text_starts_with("再看1个获得");
    if (ui_text_2) {
      console.log("treasure after: 找到【再看1个获得】");
      utils.click_text_starts_with("再看1个获得");
      sleep(2 * 1000);
      this.execute();
      return;
    }
    var ui_text_3 = utils.exists_text_starts_with("换个视频");
    if (ui_text_3) {
      console.log("treasure after: 找到【换个视频】");
      utils.click_text_starts_with("换个视频");
      sleep(2 * 1000);
      this.execute();
      return;
    }

    console.log("treasure after: 没找到【再看一个领取】");
    this.next();
  },
  next() {
    console.log("treasure next");
    if (utils.exists_text_starts_with("看广告再")) {
      console.log("treasure next: 找到【看广告再】");
      this.init();
      return;
    }
    if (utils.click_text("开心收下")) {
      console.log("treasure next: 找到【开心收下】");
      sleep(2 * 1000);
    }
    if (utils.click_text("立即收下")) {
      console.log("treasure next: 找到【立即收下】");
      sleep(2 * 1000);
    }
    this.start();
  },
};

// treasure.go_page_from_home();
utils.click_size_x_y(24 + (696 - 24) / 2, 1020);
